from django.shortcuts import render
from rest_framework import viewsets, permissions
from rest_framework import viewsets, filters, pagination
from drf_haystack.serializers import HaystackSerializer
from drf_haystack.viewsets import HaystackViewSet
# Create your views here.

from api.serializers import blog
from blog import models
from blog.search_indexes import ArticleIndex

# 记得要导包啊


class PageSet(pagination.PageNumberPagination):
    # 每页显示多少个
    page_size = 10
    # 默认每页显示10个
    page_size_query_param = "size"
    # 最大页数
    max_page_size = 10
    # 获取页码数的
    page_query_param = "page"

class ArticleViewSet(viewsets.ModelViewSet):
    """
    博文管理
    """
    queryset = models.Article.objects.all().order_by('-create_time')
    serializer_class = blog.ArticleSerializer
    # permission_classes = (permissions.IsAuthenticated,)
    # 指定分页配置
    pagination_class = PageSet


class CategroyViewSet(viewsets.ModelViewSet):
    """
    分类
    """
    queryset = models.Category.objects.all()
    serializer_class = blog.CategroySerializer
    # permission_classes = (permissions.IsAuthenticated,)

class TagViewSet(viewsets.ModelViewSet):
    """
    标签
    """
    queryset = models.Tag.objects.all().order_by('id')
    serializer_class = blog.TagSerializer
    permission_classes = (permissions.IsAuthenticated,)


class ActivateViewSet(viewsets.ModelViewSet):
    """
    公告管理
    """
    queryset = models.Activate.objects.all().order_by('id')
    serializer_class = blog.ActivateSerializer
    permission_classes = (permissions.IsAuthenticated,)


class CarouselViewSet(viewsets.ModelViewSet):
    """
    轮播图管理
    """
    queryset = models.Carousel.objects.all().order_by('id')
    serializer_class = blog.CarouselSerializer
    # permission_classes = (permissions.IsAuthenticated,)



class ArticleSerializer(HaystackSerializer):

    class Meta:
        # The `index_classes` attribute is a list of which search indexes
        # we want to include in the search.
        index_classes = [ArticleIndex]

        # The `fields` contains all the fields we want to include.
        # NOTE: Make sure you don't confuse these with model attributes. These
        # fields belong to the search index!
        fields = [
            "text", "address", "city", "zip_code", "autocomplete"
        ]


class ArticleSearchView(HaystackViewSet):

    # `index_models` is an optional list of which models you would like to include
    # in the search result. You might have several models indexed, and this provides
    # a way to filter out those of no interest for this particular view.
    # (Translates to `SearchQuerySet().models(*index_models)` behind the scenes.
    index_models = [models.Article]

    serializer_class = ArticleSerializer